/*
 *  Copyright (C) 2010  Noxious Productions
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#include "xml_file.hpp"

XMLFile::XMLFile()
{
    root = 0;
    active = 0;
    error = false;
}

XMLFile::~XMLFile()
{
    doc.SaveFile();
    root = 0;
    active = 0;
}

bool XMLFile::LoadFile(const string &fileName)
{
    if (!doc.LoadFile(fileName.c_str(), TIXML_DEFAULT_ENCODING))
    {
        if (doc.Error())
            cerr << doc.ErrorDesc() << endl;
        error = true;
        return false;
    }

    root = doc.RootElement();
    if (root == 0)
    {
        error = true;
        return false;
    }
    active = root;

    return true;
}

bool XMLFile::ResetToRoot()
{
    if (error)
        return false;

    active = root;
    if (active == 0)
    {
        error = true;
        return false;
    }

    return true;
}

bool XMLFile::SetActiveElement(const string &elementName)
{
    if (error)
        return false;

    TiXmlElement * element;
    element = active->FirstChildElement(elementName.c_str());
    if (element == 0)
        return false;
    active = element;

    return true;
}

bool XMLFile::Next()
{
    if (error)
        return false;

    TiXmlElement * element;
    element = active->NextSiblingElement();
    if (element == 0)
        return false;
    active = element;

    return true;
}

const char * XMLFile::GetAttribute(const std::string &attributeName)
{
    if (error)
        return "";

    const char * str = active->Attribute(attributeName.c_str());
    if (str == 0)
        return "";
    return str;
}

int XMLFile::GetAttribute(const std::string &attributeName, int defaultValue)
{
    if (error)
        return defaultValue;

    int ret;
    if (active->Attribute(attributeName.c_str(), &ret) == NULL)
        return defaultValue;
    return ret;
}

double XMLFile::GetAttribute(const std::string &attributeName, double defaultValue)
{
    if (error)
        return defaultValue;

    double ret;
    if (active->Attribute(attributeName.c_str(), &ret) == NULL)
        return defaultValue;
    
    return ret;
}

const char * XMLFile::GetText()
{
    if (error)
        return "";

    TiXmlText * Text;
    Text = active->FirstChild()->ToText();
    if (Text == 0)
        return "";

    const char * str = Text->Value();
    if (str == 0)
        return "";
    return str;
}

bool XMLFile::SetText(const string &text)
{
    if (error)
        return false;

    TiXmlText * Text;
    Text = active->FirstChild()->ToText();
    if (Text == 0)
        return false;

    Text->SetValue(text.c_str());
    return true;
}

void XMLFile::SetAttribute(const string &attributeName, const string &value)
{
    if (error)
        return;
    active->SetAttribute(attributeName.c_str(), value.c_str());
}

void XMLFile::SetAttribute(const string &attributeName, int value)
{
    if (error)
        return;
    active->SetAttribute(attributeName.c_str(), value);
}

void XMLFile::SetAttribute(const string &attributeName, double value)
{
    if (error)
        return;
    active->SetDoubleAttribute(attributeName.c_str(), value);
}
